package com.panshi.modules.system.controller;


import com.panshi.api.util.CommonResult;
import com.panshi.api.util.R;
import com.panshi.modules.system.bean.BeiSysFunction;
import com.panshi.modules.system.bean.BeiSysRole;
import com.panshi.modules.system.bean.SysRoleFunction;
import com.panshi.modules.system.service.BeiSysFunctionService;
import com.panshi.modules.system.service.BeiSysRoleService;
import com.panshi.modules.system.service.BeiSysUserRoleService;
import com.panshi.modules.system.service.SysRoleFunctionService;
import com.panshi.modules.system.util.MyTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 角色表  前端控制器
 * </p>
 *
 * @author 小伍
 * @since 2020-08-07
 */
@Controller
@Api(tags = "角色接口")
@Slf4j
public class BeiSysRoleController {

    @Autowired
    private BeiSysRoleService beiSysRoleService;

    @Autowired
    private BeiSysFunctionService beiSysFunctionService;

    @Autowired
    private SysRoleFunctionService sysRoleFunctionService;

    @Autowired
    private BeiSysUserRoleService beiSysUserRoleService;



    @ApiOperation("所有角色页面")
    @RequiresPermissions("role:list")
    @GetMapping("/role/list")
    public String admin(){
        return "/admin/admin-role";
    }


    @ApiOperation("分页查询所以角色")
    @GetMapping("/role/query")
   @RequiresPermissions("role:list")
    @ResponseBody
    public R query(Integer currentPage,Integer pageSize){
        Map<String,Integer> map=new HashMap<>();
        map.put("currentPage", currentPage);
        map.put("pageSize",pageSize);
        List<BeiSysRole> query = beiSysRoleService.query(map);
        Integer integer = beiSysRoleService.querySysRoleCount();
        return  R.ok().data("data",query).data("total",integer);
    }


    @ApiOperation("查询所以角色")
    @GetMapping("/role/querys")
   @RequiresPermissions("role:list")
    @ResponseBody
    public  List<BeiSysRole> querys(){
        return  beiSysRoleService.querys();
    }

    @ApiOperation("修改状态")
    @PostMapping("/role/updatestate")
   @RequiresPermissions("role:update")
    @ResponseBody
    public CommonResult updatestate(BeiSysRole beiSysRole){
        beiSysRole.setUpdateTime(new Date());
        beiSysRoleService.updateRoleBystate(beiSysRole);
        return new CommonResult();
    }
    
    @PostMapping("/role/add")
    @ApiOperation("新增角色")
    @RequiresPermissions("role:add")
    @ResponseBody
    public CommonResult addrole(BeiSysRole beiSysRole,Integer [] checkedarray){
        beiSysRole.setCreateTime(new Date());
        beiSysRole.setUpdateTime(new Date());
        Integer rid = beiSysRoleService.addrole(beiSysRole);//获取角色id
        for (Integer integer : checkedarray) {
            SysRoleFunction s = new SysRoleFunction();
            s.setRid(rid);
            s.setFid(integer);
            beiSysRoleService.addSysRoleFunction(s);
        }
        return  new CommonResult();
    }


    @PostMapping("/role/update")
    @ApiOperation("修改角色")
    @RequiresPermissions("role:update")
    @ResponseBody
    public CommonResult updaterole(BeiSysRole beiSysRole,Integer [] checkedarray){
        beiSysRole.setUpdateTime(new Date());
        beiSysRoleService.updateSysRole(beiSysRole);
        sysRoleFunctionService.deleteRoleFun(beiSysRole.getRid());
        for (Integer integer : checkedarray) {
            SysRoleFunction s = new SysRoleFunction();
            s.setRid(beiSysRole.getRid());
            s.setFid(integer);
            beiSysRoleService.addSysRoleFunction(s);
        }
        return new CommonResult();
    }

    @GetMapping("/role/delete")
    @ApiOperation("删除角色")
   @RequiresPermissions("role:delete")
    @ResponseBody
    public CommonResult deleteRole(Integer rid){
        beiSysRoleService.deleteSysRole(rid);//删除角色表
        sysRoleFunctionService.deleteRoleFun(rid);//删除角色菜单关联表
        beiSysUserRoleService.deleteSysRoleByrid(rid);
        return new CommonResult();
    }

}

